package com.zenithmind.user.pojo.domain.valueobject;

import lombok.Value;
import lombok.Builder;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.regex.Pattern;

/**
 * 租户联系信息值对象
 * 遵循单一职责原则：只负责联系信息管理
 * 遵循封装原则：不可变对象，确保数据一致性
 */
@Value
@Builder(toBuilder = true)
@Schema(description = "租户联系信息")
public class TenantContact {
    
    private static final Pattern EMAIL_PATTERN = Pattern.compile(
        "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
    );
    
    private static final Pattern PHONE_PATTERN = Pattern.compile(
        "^1[3-9]\\d{9}$|^\\d{3,4}-\\d{7,8}$|^\\d{7,8}$"
    );

    @Schema(description = "联系人姓名")
    String contactName;

    @Schema(description = "联系人电话")
    String contactPhone;

    @Schema(description = "联系人邮箱")
    String contactEmail;

    @Schema(description = "联系地址")
    String address;

    /**
     * 创建联系信息
     */
    public static TenantContact create(String contactName, String contactPhone, String contactEmail, String address) {
        return TenantContact.builder()
                .contactName(validateContactName(contactName))
                .contactPhone(validateContactPhone(contactPhone))
                .contactEmail(validateContactEmail(contactEmail))
                .address(validateAddress(address))
                .build();
    }

    /**
     * 创建最小联系信息（仅邮箱）
     */
    public static TenantContact minimal(String contactEmail) {
        return TenantContact.builder()
                .contactEmail(validateContactEmail(contactEmail))
                .build();
    }

    /**
     * 更新联系人姓名
     */
    public TenantContact withContactName(String contactName) {
        return this.toBuilder()
                .contactName(validateContactName(contactName))
                .build();
    }

    /**
     * 更新联系电话
     */
    public TenantContact withContactPhone(String contactPhone) {
        return this.toBuilder()
                .contactPhone(validateContactPhone(contactPhone))
                .build();
    }

    /**
     * 更新联系邮箱
     */
    public TenantContact withContactEmail(String contactEmail) {
        return this.toBuilder()
                .contactEmail(validateContactEmail(contactEmail))
                .build();
    }

    /**
     * 更新地址
     */
    public TenantContact withAddress(String address) {
        return this.toBuilder()
                .address(validateAddress(address))
                .build();
    }

    /**
     * 验证联系人姓名
     */
    private static String validateContactName(String contactName) {
        if (contactName != null && contactName.length() > 50) {
            throw new IllegalArgumentException("联系人姓名长度不能超过50个字符");
        }
        return contactName;
    }

    /**
     * 验证联系电话
     */
    private static String validateContactPhone(String contactPhone) {
        if (contactPhone != null && !contactPhone.trim().isEmpty()) {
            if (contactPhone.length() > 20) {
                throw new IllegalArgumentException("联系电话长度不能超过20个字符");
            }
            if (!PHONE_PATTERN.matcher(contactPhone).matches()) {
                throw new IllegalArgumentException("联系电话格式不正确");
            }
        }
        return contactPhone;
    }

    /**
     * 验证联系邮箱
     */
    private static String validateContactEmail(String contactEmail) {
        if (contactEmail != null && !contactEmail.trim().isEmpty()) {
            if (contactEmail.length() > 100) {
                throw new IllegalArgumentException("联系邮箱长度不能超过100个字符");
            }
            if (!EMAIL_PATTERN.matcher(contactEmail).matches()) {
                throw new IllegalArgumentException("联系邮箱格式不正确");
            }
        }
        return contactEmail;
    }

    /**
     * 验证地址
     */
    private static String validateAddress(String address) {
        if (address != null && address.length() > 500) {
            throw new IllegalArgumentException("地址长度不能超过500个字符");
        }
        return address;
    }

    /**
     * 检查联系信息是否完整
     */
    public boolean isComplete() {
        return contactName != null && !contactName.trim().isEmpty() &&
               contactEmail != null && !contactEmail.trim().isEmpty();
    }

    /**
     * 检查是否有有效的联系方式
     */
    public boolean hasValidContact() {
        return (contactPhone != null && !contactPhone.trim().isEmpty()) ||
               (contactEmail != null && !contactEmail.trim().isEmpty());
    }

    /**
     * 获取主要联系方式描述
     */
    public String getPrimaryContactDescription() {
        if (contactEmail != null && !contactEmail.trim().isEmpty()) {
            return "邮箱: " + contactEmail;
        }
        if (contactPhone != null && !contactPhone.trim().isEmpty()) {
            return "电话: " + contactPhone;
        }
        return "无联系方式";
    }
}
